package com.intelligent.ispc.foundation.pdf.impl;

import com.intelligent.ispc.foundation.pdf.PdfComponentFactory;
import com.intelligent.ispc.foundation.pdf.PdfCreator;
import com.intelligent.ispc.foundation.pdf.TableStyleConfig;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eligi.Ran on 2016/9/2.
 */
public class PdfCreatorImpl implements PdfCreator {

    @Override
    public void createSinglePage(PdfPTable paramTable, PdfPTable dataTable, Image image, String savePath) {
        if (paramTable == null || dataTable == null || image == null || savePath == null) {
            throw new NullPointerException();
        }
        Document document = new Document();
        File pdfFile = createPdfFile(savePath);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
            PdfPTable pdfPTable = new PdfPTable(2);
            float[] tableWidth = {1.2f, 4};
            pdfPTable.setWidths(tableWidth);
            PdfPCell paramCell = new PdfPCell(paramTable);
            paramCell.setBorder(Rectangle.NO_BORDER);
            paramCell.setHorizontalAlignment(Element.ALIGN_LEFT);

            PdfPCell imageCell = new PdfPCell(image);
            imageCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            imageCell.setBorder(Rectangle.NO_BORDER);

            PdfPCell dataCell = new PdfPCell(dataTable);
            dataCell.setColspan(2);

            PdfPCell emptyCell = new PdfPCell(new Paragraph("   "));
            emptyCell.setColspan(2);
            emptyCell.setBorder(Rectangle.NO_BORDER);

            pdfPTable.addCell(paramCell);
            pdfPTable.addCell(imageCell);
            pdfPTable.addCell(emptyCell);
            pdfPTable.addCell(dataCell);

            document.open();
            document.add(pdfPTable);
            document.close();
            writer.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void createMultiPage(List<PdfPTable> paramTable, List<PdfPTable> dataTable, List<Image> image, String savePath) {
        if (paramTable == null || dataTable == null || image == null || savePath == null) {
            throw new NullPointerException();
        }
    }

    public static void main(String[] args) throws DocumentException {
        List<String> metadatas = new ArrayList<>();
        metadatas.add("TestFiled 1");
        metadatas.add("TestFiled 2");
        Object[][] objects = new Object[3][2];
        for (int i = 0; i < objects.length; i++) {
            objects[i][0] = " Test Value 1" + (i + 1);
            objects[i][1] = " Test Value 2" + (i + 1);
        }
        PdfPTable dataTable = PdfComponentFactory.paintTable(metadatas, objects, new TableStyleConfig(null));
        List<String> paramMetadatas = new ArrayList<>();
        paramMetadatas.add("TestParam 1");
        paramMetadatas.add("TestParam 2");
        Object[][] paramObjects = new Object[1][2];
        paramObjects[0][0] = " Param 1";
        paramObjects[0][1] = " Param 2";
        PdfPTable paramTable = PdfComponentFactory.paintTable(paramMetadatas, paramObjects, new TableStyleConfig(1));
        Image image = PdfComponentFactory.paintImage("E:/images.png", null);

        Font font = new Font();
        font.setSize(5);
        float[] paramTableWidth = {2, 1.5f};
        paramTable.setWidths(paramTableWidth);
        image.setAbsolutePosition(100f, 500f);

        PdfCreator pdfCreator = new PdfCreatorImpl();
        String savePath = "E:/hello.pdf";
        pdfCreator.createSinglePage(paramTable, dataTable, image, savePath);

    }

    private File createPdfFile(String pdfPath) {
        File pdfFile = new File(pdfPath);
        if (pdfFile.exists()) {
            pdfFile.delete();
        }
        try {
            pdfFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pdfFile;
    }
}
